<?php

namespace Biz\model\advertisement;


class MUserprofileLabels extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'userprofile_labels';

    public static function all($size = 10000)
    {
        $all = [];
        $p = 0;
        while (true) {
            $where = ['status' => self::STATUS_NORMAL, 'LIMIT' => [$p * $size, $size]];
            $list = self::select(['id', 'uid', 'labels'], $where);
            if (empty($list)) break;
            $p ++;
            $all = array_merge($all, $list);
        }
        return $all;
    }


    public static function create($data)
    {
        $time = time();
        $insertData = [
            'uid' => $data['uid'],
            'labels' => $data['labels'],
            'addtime' => $time,
            'edittime' => $time,
            'status' => self::STATUS_NORMAL,
        ];
        self::insert($insertData);
    }

    public static function modify($id, $data)
    {
        $data['edittime'] = time();
        self::update($data, ['id' => $id]);
    }

    public static function addLabelByUid($uid, $label)
    {
        $labels = self::get('labels', ['uid' => $uid]);

        $labels = $labels? explode(',', $labels) : [];

        if (in_array($label, $labels)) return true;

        $labels[$label] = $label;
        sort($labels);

        self::update(['labels' => implode(',', $labels)], ['uid' => $uid]);
    }
}